package com.fhnew.app.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;

import android.graphics.drawable.GradientDrawable;
import android.util.Base64;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.fhnew.app.R;

public class CheckOutView extends LinearLayout {

    //图像显示部分
    private FrameLayout imgLayout;
    private ImageView bgImage;
    private int bgViewWidth; // 显示背景图像控件的宽度
    private int bgViewHeight; // 显示背景图像控件的高度
    private ImageView blockImage;
    private int blockViewWidth; //显示滑块图像的宽度
    private int blockViewHeight; //显示滑块图像的高度

    private int seekBarLayoutHeight = 30; //seekBar部分布局的高度 单位 dp
    private int seekBarHeight = 25;//seekBar的高度

    private String bgUrl;//传入图像的URL
    private int bgWidth;//传入图像的宽度
    private int bgHeight;//传入图像的高度

    private String blockUrl;//传入滑块图像的URL
    private int blockWidth;//传入滑块图像的宽度
    private int blockHeight; //传入滑块图像的高度

    private int blockY; //滑块Y轴的高度

    private CheckOutListener checkOutListener;
    private float seekValue = 0;

    private DisplayMetrics displayMetrics;

    //底部拖动条
    private CheckOutSeekView seekBar;

    public CheckOutView(Context context, String bgUrl, int bgWidth, int bgHeight, String blockUrl, int blockWidth, int blockHeight, int blockY) {
        super(context);
        this.bgUrl = bgUrl;
        this.bgWidth = bgWidth;
        this.bgHeight = bgHeight;
        this.blockUrl = blockUrl;
        this.blockWidth = blockWidth;
        this.blockHeight = blockHeight;
        this.blockY = blockY;

        init();
    }

    private void loadImage(String code,ImageView codeImg){
        Bitmap bitmap = null;
        byte[] bitmapArray;
        bitmapArray = Base64.decode(code, Base64.NO_WRAP);
        bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
        codeImg.setImageBitmap(bitmap);//显示图片
    }

    private void init() {

        displayMetrics = getResources().getDisplayMetrics();

        //垂直布局
        setOrientation(LinearLayout.VERTICAL);

        //背景图片显示部分
        imgLayout = new FrameLayout(getContext());
        LinearLayout.LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        imgLayout.setLayoutParams(layoutParams);

        bgImage = new ImageView(getContext());
        FrameLayout.LayoutParams fLayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, 300);
        bgImage.setLayoutParams(fLayoutParams);
        bgImage.setBackground(new ColorDrawable(Color.argb(255,161,161,161)));
        bgImage.setScaleType(ImageView.ScaleType.FIT_XY);

        //Glide.with(getContext()).load(bgUrl).placeholder(R.drawable.loading_img).into(bgImage);//.placeholder() //设置加载过程中的图片
        loadImage(bgUrl,bgImage);

        imgLayout.addView(bgImage);

        blockImage = new ImageView(getContext());
        fLayoutParams = new FrameLayout.LayoutParams(blockViewWidth, blockViewHeight);
        fLayoutParams.setMargins(0, blockY, 0, 0);
        blockImage.setLayoutParams(fLayoutParams);
        blockImage.setBackground(new ColorDrawable(Color.argb(255,43,43,43)));
        blockImage.setScaleType(ImageView.ScaleType.FIT_XY);

        //Glide.with(getContext()).load(blockUrl).placeholder(R.drawable.loading_img).into(blockImage);
        loadImage(blockUrl,blockImage);

        imgLayout.addView(blockImage);
        addView(imgLayout);

        // 添加底部拖动条
        FrameLayout seekBarLayout = new FrameLayout(getContext());
        layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, (int) (displayMetrics.density * seekBarLayoutHeight));
        seekBarLayout.setLayoutParams(layoutParams);

        seekBar = new CheckOutSeekView(getContext());
        fLayoutParams = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int) (displayMetrics.density*seekBarHeight));
        fLayoutParams.gravity = Gravity.CENTER;
        seekBar.setLayoutParams(fLayoutParams);
        seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);

        seekBarLayout.addView(seekBar);

        addView(seekBarLayout);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //当前控件的尺寸
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        bgViewWidth = width;
        bgViewHeight = (int) (height - displayMetrics.density * seekBarLayoutHeight);

        //重新计算尺寸
        LinearLayout.LayoutParams layoutParams = (LayoutParams) imgLayout.getLayoutParams();
        layoutParams.width = bgViewWidth;
        layoutParams.height = bgViewHeight;
        imgLayout.setLayoutParams(layoutParams);

        FrameLayout.LayoutParams fLayoutParams = (FrameLayout.LayoutParams) bgImage.getLayoutParams();
        fLayoutParams.width = bgViewWidth;
        fLayoutParams.height = bgViewHeight;
        bgImage.setLayoutParams(fLayoutParams);

        //
        fLayoutParams = (FrameLayout.LayoutParams) blockImage.getLayoutParams();
        float zoneWidth = ((float) bgWidth / (bgViewWidth == 0 ? 1 : (float) bgViewWidth));
        float zoneHeight = ((float) bgHeight / (bgViewHeight == 0 ? 1 : (float) bgViewHeight));
        fLayoutParams.topMargin = (int) (blockY / zoneHeight);
        fLayoutParams.width = (int) (blockWidth / zoneWidth);
        fLayoutParams.height = (int) (blockHeight / zoneHeight);

        blockViewWidth = fLayoutParams.width;
        blockViewHeight = fLayoutParams.height;

        blockImage.setLayoutParams(fLayoutParams);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    private CheckOutSeekView.OnSeekBarChangeListener onSeekBarChangeListener = new CheckOutSeekView.OnSeekBarChangeListener() {

        @Override
        public void onProgressChanged(CheckOutSeekView seekBar, int progress) {
            // HLog.I("修改进度"+progress);
            seekValue = progress;
            FrameLayout.LayoutParams fLayoutParams = (FrameLayout.LayoutParams) blockImage.getLayoutParams();
            fLayoutParams.setMargins(progress * (bgViewWidth - blockViewWidth) / 100, blockY, 0, 0);
            blockImage.setLayoutParams(fLayoutParams);
        }

        @Override
        public void onStartTrackingTouch(CheckOutSeekView seekBar) {

        }

        @Override
        public void onStopTrackingTouch(CheckOutSeekView seekBar) {

            if (checkOutListener != null) {
                float seekViewValue = (float) (seekValue * ((float) (bgViewWidth - blockViewWidth) / 100.0));// seek 滑动对应的控件尺寸
                float seekValue = seekViewValue * ((float) bgWidth / (float) bgViewWidth);// seek 滑动对应的图像位置
                checkOutListener.seekEnd(seekValue);
            }

            seekBar.setProgress(0);
            FrameLayout.LayoutParams fLayoutParams = (FrameLayout.LayoutParams) blockImage.getLayoutParams();
            fLayoutParams.setMargins(0, blockY, 0, 0);
            blockImage.setLayoutParams(fLayoutParams);
        }
    };

    public void setCheckOutListener(CheckOutListener checkOutListener) {
        this.checkOutListener = checkOutListener;
    }

    // 移动结束监听
    public interface CheckOutListener {
        public void seekEnd(float value);
    }


    //=================================================================================================
    // 自定义滑动条
    //=================================================================================================

    public static class CheckOutSeekView extends FrameLayout {

        private ImageView moveImage;
        private FrameLayout moveLayout;

        private FrameLayout coverLayout_Left;
        private FrameLayout coverLayout_Right;
        private TextView hintView;

        private int width; //当前空间的宽度
        private int height; //当前空间的高度

        private int seekValue = 0;//当前滑块的位置
        private int seekWidth;//滑块的宽度

        private boolean moving  = false;//当前滑块移动状态
        private float moveStartLocation = 0;
        private int progress; //当前的进度

        private String hintText ="向右拖动验证";
        private OnSeekBarChangeListener onSeekBarChangeListener;


        public CheckOutSeekView(Context context) {
            super(context);
            init();
        }

        private void init() {
            GradientDrawable radiusDrawable = new GradientDrawable();
            radiusDrawable.setCornerRadius(10);
            radiusDrawable.setColor(Color.argb(255, 122, 194, 60));

            // 左侧遮盖部分
            coverLayout_Left = new FrameLayout(getContext());
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(seekValue, LayoutParams.MATCH_PARENT);
            coverLayout_Left.setLayoutParams(layoutParams);
            coverLayout_Left.setBackground(radiusDrawable);

            addView(coverLayout_Left);

            radiusDrawable = new GradientDrawable();
            radiusDrawable.setCornerRadius(10);
            radiusDrawable.setColor(Color.argb(255, 232, 232, 232));

            // 右侧遮盖部分
            coverLayout_Right = new FrameLayout(getContext());
            layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            layoutParams.leftMargin = seekValue;
            coverLayout_Right.setLayoutParams(layoutParams);
            coverLayout_Right.setBackground(radiusDrawable);

            hintView = new TextView(getContext());
            layoutParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            hintView.setText(hintText);
            layoutParams.gravity = Gravity.CENTER;
            hintView.setLayoutParams(layoutParams);

            coverLayout_Right.addView(hintView);

            addView(coverLayout_Right);

            // 滑块部分
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setCornerRadius(10);
            gradientDrawable.setColor(Color.argb(255, 203, 203, 203));


            moveLayout = new FrameLayout(getContext());
            layoutParams = new LayoutParams(80, LayoutParams.MATCH_PARENT);
            layoutParams.leftMargin = seekValue;
            moveLayout.setLayoutParams(layoutParams);
            moveLayout.setBackground(gradientDrawable);
            moveLayout.setPadding(10, 10, 10, 10);

            moveImage = new ImageView(getContext());
            layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            layoutParams.leftMargin = 0;
            moveImage.setLayoutParams(layoutParams);
            moveImage.setBackgroundResource(R.drawable.move_img);

            moveLayout.addView(moveImage);

            addView(moveLayout);

        }

        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            width = MeasureSpec.getSize(widthMeasureSpec);
            height = MeasureSpec.getSize(heightMeasureSpec);

            seekWidth = (int) (height*1.4);

            // 重新测量滑块宽度
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) moveLayout.getLayoutParams();
            layoutParams.width = seekWidth;
            moveLayout.setLayoutParams(layoutParams);

        }


        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    if(event.getX()>= seekValue && event.getX() <= seekValue+seekWidth ){
                        moveStartLocation = event.getX();
                        moving = true;
                        if (onSeekBarChangeListener != null){
                            onSeekBarChangeListener.onStartTrackingTouch(this);
                        }
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if(moving){
                        hintView.setVisibility(GONE);

                        float moveValue = (int) (seekValue+(event.getX()-moveStartLocation));
                        if (moveValue < 0){
                            moveValue = 0;
                        }else if(moveValue> width-seekWidth){
                            moveValue = width-seekWidth;
                        }
                        setViewProgress(moveValue);
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    if(moving){

                        seekValue = (int) (seekValue+(event.getX()-moveStartLocation));
                        if (seekValue < 0){
                            seekValue = 0;
                        }else if(seekValue> width-seekWidth){
                            seekValue = width-seekWidth;
                        }

                        if(seekValue == 0){
                            hintView.setVisibility(VISIBLE);
                        }

                        if (onSeekBarChangeListener != null){
                            onSeekBarChangeListener.onStopTrackingTouch(this);
                        }

                        moveStartLocation = 0;
                        moving = false;
                    }
                    break;
            }
            return true;
        }

        public void setViewProgress(float seekValue) {
            FrameLayout.LayoutParams  layoutParams = (FrameLayout.LayoutParams) moveLayout.getLayoutParams();
            layoutParams.leftMargin = (int) seekValue;
            moveLayout.setLayoutParams(layoutParams);

            layoutParams = (FrameLayout.LayoutParams) coverLayout_Left.getLayoutParams();
            layoutParams.width = (int) (seekValue+seekWidth/2);
            coverLayout_Left.setLayoutParams(layoutParams);

            layoutParams = (FrameLayout.LayoutParams) coverLayout_Right.getLayoutParams();
            layoutParams.leftMargin =  (int)(seekValue+seekWidth/2);
            coverLayout_Right.setLayoutParams(layoutParams);

            this.progress = (int) (seekValue/(width-seekWidth)*100);

            if (onSeekBarChangeListener != null){
                onSeekBarChangeListener.onProgressChanged(this,this.progress);
            }
        }

        // 设置滑块的进度
        public void setProgress(int progress) {
            this.progress = progress;
            float moveValue = progress*((width-seekWidth)/100);
            this.seekValue = (int) moveValue;
            if(seekValue == 0){
                hintView.setVisibility(VISIBLE);
            }
            setViewProgress(moveValue);
        }

        public void setOnSeekBarChangeListener(OnSeekBarChangeListener onSeekBarChangeListener){
            this.onSeekBarChangeListener = onSeekBarChangeListener;
        }

        public interface OnSeekBarChangeListener {

            void onProgressChanged(CheckOutSeekView seekBar, int progress);

            void onStartTrackingTouch(CheckOutSeekView seekBar);

            void onStopTrackingTouch(CheckOutSeekView seekBar);
        }
    }

}




